package com.zhipu.balance.ui.vm.home.calculate.wheat

import android.text.TextUtils
import androidx.databinding.ObservableField
import com.zhipu.balance.mobile.event.Personnel
import com.zhipu.balance.mobile.event.RecordWheat
import com.zhipu.balance.remotedata.systemapi.SystemApiService
import com.zhipu.balance.ui.vm.BaseViewModel
import com.zhipu.balance.utils.CommonUtils

/**
 * 小麦杂质
 */
class WheatImpurityViewModel(mSystemApiService : SystemApiService) : BaseViewModel(mSystemApiService){


    /**
     * 重量
     */
    var weighing : String? = null

    /**
     * 小麦实体
     */
    var mRecordWheat : RecordWheat? = null


    /**
     * 当前的检查人员
     */
    var mPersonnel : Personnel? = null


    /**
     * 大样质量
     */
    var largeMiscellaneousSampleQuality = ObservableField("")
    /**
     * 大样杂质
     */
    var detailImpurity = ObservableField("")

    /**
     * 小样试验-试样质量
     */
    var smallMiscellaneousSampleQuality = ObservableField("")
    /**
     * 小样试验-杂质
     */
    var smallSampleImpurity = ObservableField("")

    /**
     * 矿物质质量
     */
    var mineralQuality = ObservableField("")




    /**
     * 大样杂质率
     */
    var detailResults = ObservableField("")


    /**
     * 小样杂质率
     */
    var sampleResults = ObservableField("")


    /**
     * 总杂质率
     */
    var impurityResults = ObservableField("")


    /**
     * 矿物质含量
     */
    var mineralContent = ObservableField("")





    /**
     * 更新页面
     */
    fun updateRecordRice(){
        if(!TextUtils.isEmpty(mRecordWheat?.impurityRecorder)){
            //大样试验-试样质量
            largeMiscellaneousSampleQuality.set(mRecordWheat?.largeMiscellaneousSampleQuality.toString())
            //大样试验-杂质
            detailImpurity.set(mRecordWheat?.detailImpurity.toString())
            //小样试验-试样质量
            smallMiscellaneousSampleQuality.set(mRecordWheat?.smallMiscellaneousSampleQuality.toString())
            //小样试验-杂质
            smallSampleImpurity.set(mRecordWheat?.smallSampleImpurity.toString())
            //矿物质质量
            mineralQuality.set(mRecordWheat?.mineralQuality.toString())


            //大样杂质率
            detailResults.set(mRecordWheat?.detailResults.toString())
            //小样杂质率
            sampleResults.set(mRecordWheat?.sampleResults.toString())
            //总杂质率
            impurityResults.set(mRecordWheat?.impurityResults.toString())
            //矿物质含量
            mineralContent.set(mRecordWheat?.mineralContent.toString())
        }
    }


    /**
     * 释放更新页面
     */
    fun releaseRecordRice(){
        //大样试验-试样质量
        largeMiscellaneousSampleQuality.set("")
        //大样试验-杂质
        detailImpurity.set("")
        //小样试验-试样质量
        smallMiscellaneousSampleQuality.set("")
        //小样试验-杂质
        smallSampleImpurity.set("")
        //矿物质质量
        mineralQuality.set("")


        //大样杂质率
        detailResults.set("")
        //小样杂质率
        sampleResults.set("")
        //总杂质率
        impurityResults.set("")
        //矿物质含量
        mineralContent.set("")

    }




    /**
     * 大样质量
     */
    fun BulkQuality(){
        largeMiscellaneousSampleQuality.set(weighing)
        mRecordWheat?.largeMiscellaneousSampleQuality = largeMiscellaneousSampleQuality.get()?.toDouble()
    }

    /**
     * 大样杂质
     */
    fun LargeImpurity(){
        detailImpurity.set(weighing)
        mRecordWheat?.detailImpurity = detailImpurity.get()?.toDouble()
    }

    /**
     * 小样质量
     */
    fun SampleQuality(){
        smallMiscellaneousSampleQuality.set(weighing)
        mRecordWheat?.smallMiscellaneousSampleQuality = smallMiscellaneousSampleQuality.get()?.toDouble()
    }


    /**
     * 小样杂质
     */
    fun SmallImpurity(){
        smallSampleImpurity.set(weighing)
        mRecordWheat?.smallSampleImpurity = smallSampleImpurity.get()?.toDouble()
    }

    /**
     * 矿物质质量
     */
    fun MineralQuality(){
        mineralQuality.set(weighing)
        mRecordWheat?.mineralQuality = mineralQuality.get()?.toDouble()
    }





    /**
     * 计算
     */
    fun calculation(){
        /**
         * 计算大样杂质率
         */
        var largeImpurityRate = CommonUtils.formatDouble((mRecordWheat!!.detailImpurity / mRecordWheat!!.largeMiscellaneousSampleQuality) * 100)
        mRecordWheat?.detailResults = largeImpurityRate
        detailResults.set(largeImpurityRate.toString())
        /**
         * 计算小样杂质率
         */
        var sampleImpurityRate = CommonUtils.formatDouble((mRecordWheat!!.smallSampleImpurity / mRecordWheat!!.smallMiscellaneousSampleQuality) * (100 - largeImpurityRate))
        mRecordWheat?.sampleResults = sampleImpurityRate
        sampleResults.set(sampleImpurityRate.toString())
        /**
         * 计算杂质总量
         */
        var totalImpurity = CommonUtils.formatDouble(largeImpurityRate + sampleImpurityRate)
        mRecordWheat?.impurityResults = totalImpurity
        impurityResults.set(totalImpurity.toString())



        /**
         * 计算大样
         */
        var details = 100 - mRecordWheat!!.detailResults
        /**
         * 计算小样
         */
        var smallKind =  mRecordWheat!!.mineralQuality / mRecordWheat!!.smallMiscellaneousSampleQuality
        /**
         * 矿物质含量
         */
        var mMineralContent = CommonUtils.formatDouble((details * smallKind))
        mRecordWheat?.mineralContent = mMineralContent
        mineralContent.set(mMineralContent.toString())


        /**
         * 杂质记录员
         */
        mRecordWheat?.impurityRecorder = mPersonnel!!.name


        //大样质量
        if(mRecordWheat?.largeMiscellaneousSampleQuality == 0.00){
            largeMiscellaneousSampleQuality.set("0.00")
        }
        //大样杂质
        if(mRecordWheat?.detailImpurity == 0.00){
            detailImpurity.set("0.00")
        }
        //小样质量
        if(mRecordWheat?.smallMiscellaneousSampleQuality == 0.00){
            smallMiscellaneousSampleQuality.set("0.00")
        }
        //小样杂质
        if(mRecordWheat?.smallSampleImpurity == 0.00){
            smallSampleImpurity.set("0.00")
        }
        //矿物质质量
        if(mRecordWheat?.mineralQuality == 0.00){
            mineralQuality.set("0.00")
        }

    }


}